#include "myreply.h"

myReply::myReply(const QUrl & url)
    : QNetworkReply()
{
    open(ReadOnly | Unbuffered);

    QString fileName;
    QByteArray (*translatePage)(QByteArray);
    QString contentType = "text/html; charset=utf-8";
    if(url.toString() == "myspaces://mainpage") {
        fileName = ":/myspaces/htmls/mainpage.html";
        translatePage = &mainPage;
    }
    else if (url.toString() == "myspaces://settings") {
        fileName = ":/myspaces/htmls/settings.html";
        translatePage = &settingsPage;
    }
    else if (url.toString() == "myspaces://manageusers") {
        fileName = ":/myspaces/htmls/manageusers.html";
        translatePage = &noTranslate;
    }
    else if(url.toString() == "myspaces://style/bs.css")
    {
        fileName = ":/myspaces/style/bs140.css";
        translatePage = &noTranslate;
        contentType = "text/css; charset=utf-8";
    }
    else if(url.toString() == "myspaces://javascript/jquery.json.js")
    {
        fileName = ":/myspaces/javascript/jquery.json-2.2.min.js";
        translatePage = &noTranslate;
        contentType = "text/javascript; charset=utf-8";
    }
    else if(url.toString() == "myspaces://javascript/jquery.js")
    {
        fileName = ":/myspaces/javascript/jquery.min.1.7.js";
        translatePage = &noTranslate;
        contentType = "text/javascript; charset=utf-8";
    }
    else if(url.toString() == "myspaces://icons/spaces.png")
    {
        fileName = ":/myspaces/icons/spaces.png";
        translatePage = &noTranslate;
        contentType = "image/png";
    }
    else {
        fileName = ":/myspaces/htmls/blank.html";
        translatePage = &noTranslate;
    }
    QFile file(fileName);
    file.open(QIODevice::ReadOnly);
    QByteArray page = file.readAll();
    file.close();

    page = translatePage(page);

    content = QByteArray(page); // fill with character 't'
    setHeader(QNetworkRequest::ContentTypeHeader,
                    QVariant(contentType));
    setHeader(QNetworkRequest::ContentLengthHeader,
                    QVariant(content.size()));

    offset = 0;

    QTimer::singleShot(0, this, SIGNAL(metaDataChanged()));
    QTimer::singleShot(0, this, SIGNAL(readyRead()));
    QTimer::singleShot(0, this, SIGNAL(finished()));

}

QByteArray myReply::mainPage(QByteArray inpage)
{
    QString page = inpage;
    page.replace("%PASSWORD-LABEL%",tr("Password"));
    page.replace("%LOGIN-LABEL%",tr("Login"));
    page.replace("%REMEMBER-LABEL%",tr("Remember"));
    page.replace("%ENTER-LABEL%",tr("Enter"));
    page.replace("%MANAGE-USERS-LABEL%",tr("Manage users"));
    page.replace("%VERSION%",APP_VERSION);
    page.replace("%SETTINGS-LABEL%", tr("Settings"));
    return page.toUtf8();
}

QByteArray myReply::settingsPage(QByteArray inpage)
{
    QString page = inpage;
    page.replace("%SETTINGS-LABEL%", tr("Settings"));
    page.replace("%PROXY-SETTINGS-LABEL%", tr("Proxy settings"));
    page.replace("%VERSION%",APP_VERSION);
    return page.toUtf8();
}

QByteArray myReply::noTranslate(QByteArray inpage)
{
    return inpage;
}

qint64 myReply::bytesAvailable() const
{
    qint64 bc = QIODevice::bytesAvailable() + content.size() - offset;
    return bc;
}

void myReply::abort()
{
}


bool myReply::isSequential() const
{
    return true;
}

qint64 myReply::readData(char *data, qint64 maxSize)
{
    if (offset < content.size()) {
        qint64 number = qMin(maxSize, content.size() - offset);
        memcpy(data, content.constData() + offset, number);
        offset += number;
        return number;
    } else
        return -1;
}
